package com.orchard.xpz.service.incomeInfo.Impl;

import com.github.pagehelper.PageHelper;
import com.orchard.xpz.dao.IncomeInfoDao;
import com.orchard.xpz.entity.IncomeInfo.IncomeInfo;
import com.orchard.xpz.entity.IncomeInfo.IncomeInfo;
import com.orchard.xpz.service.incomeInfo.IncomeInfoService;
import com.orchard.xpz.utils.PageResultData;
import com.orchard.xpz.utils.UserRuntime;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @TaskCode:
 * @description:
 * @author: minshuai.chen
 * @create: 2022/4/21 16:39
 **/
@Service
public class IncomeInfoServiceImpl implements IncomeInfoService {

    @Autowired
    private IncomeInfoDao  incomeInfoDao;

    @Override
    public PageResultData<IncomeInfo> getPageList(IncomeInfo incomeInfo) {
        PageHelper.startPage(incomeInfo.getPageNum(),incomeInfo.getPageSize());
        List<IncomeInfo> list = incomeInfoDao.getList(incomeInfo);
        PageResultData<IncomeInfo> pageResultData = new PageResultData<>(list, incomeInfo.getPageNum(), incomeInfo.getPageSize());
        return pageResultData;
    }

    @Override
    public boolean saveOrUpdate(IncomeInfo incomeInfo) {
        incomeInfo.setGardenId(incomeInfo.getGardenPlot().getGardenId());
        if (ObjectUtils.isEmpty(incomeInfo.getIncomeId())){
            incomeInfo.setCreateName(UserRuntime.getUser().getUserName());
            incomeInfo.setCreateTime(LocalDate.now());
            int i = incomeInfoDao.insertSelective(incomeInfo);
            return i > 0? true : false;
        }else {
            incomeInfo.setUpdateTime(LocalDate.now());
            incomeInfo.setUpdateName(UserRuntime.getUser().getUserName());
            int i = incomeInfoDao.updateByPrimaryKeySelective(incomeInfo);
            return i > 0? true : false;
        }
    }

    @Override
    public boolean deltePlot(String id) {
        return incomeInfoDao.deleteByPrimaryKey(id) > 0? true : false;
    }
}
